package com.knoya.chejian.pda.module.outsourcing

import android.app.ActivityManager
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Vibrator
import android.util.Log
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.util.FileUtils
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_outsourcing.*
import kotlinx.android.synthetic.main.activity_ruku.*
import kotlinx.android.synthetic.main.header_knoya.*

class OutsourcingActivity : BaseActivity(), View.OnClickListener,OutsourcingContract.OutsourcingView {


    var issaoyunxu = true
    lateinit var scans : ArrayList<String>
    lateinit var P : OutsourcingP
    lateinit var updialog: AlertDialog
    lateinit var adapter : OutSourcingAdapter
    override fun getLayoutId(): Int {
        return R.layout.activity_outsourcing;
    }

    override fun initCreate() {
        tv_header_title.text = "外协发货扫描"
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        BtTijiao.setOnClickListener(this)
        BtShanchu.setOnClickListener(this)
        scans = ArrayList<String>()
        adapter = OutSourcingAdapter(this,scans)
        LvOutsoursing.adapter = adapter
        P = OutsourcingP(scans,this,this)
        mBoradcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if(!isAPPBroughtToBackground()){
                    return
                }
                if(!issaoyunxu){
                    return
                }
                if (intent.action == "nlscan.action.SCANNER_RESULT" && intent.hasExtra("SCAN_BARCODE1")) {
                    val scanResult_1 = intent.getStringExtra("SCAN_BARCODE1").trim()
                    samiaoOk(scanResult_1)
                }
                if(intent.action == "android.intent.ACTION_DECODE_DATA" && intent.hasExtra("barcode_string")){
                    val scanResult = intent.getStringExtra("barcode_string").trim()
                    samiaoOk(scanResult)
                }
                if(intent.action == "com.qs.scancode"){
                    val scanResult = intent.extras.getString("data").trim()
                    samiaoOk(scanResult)
                }
            }
        }
    }

    //扫描获取到数据后处理
    fun samiaoOk(data : String){
        var d = data;
        if(data.length>28){
            var ss = data.split("#")
            if(ss.size > 1){
                d = ss[1]
            }
        }
        if(d.length>28){
            Myutil.tishidialog(this@OutsourcingActivity,"条码扫描错误，请重新扫描")
            Myutil.zhendong(this)
            FileUtils.playSound(R.raw.baojing);
        }
        if(!scans.contains(d)){
            scans.add(d)
            adapter.notifyDataSetChanged()
        }
    }

    fun isAPPBroughtToBackground(): Boolean {
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val tasks = am.getRunningTasks(1)
        if (!tasks.isEmpty()) {
            val topActivity = tasks[0].topActivity
            if (topActivity.className == this.javaClass.name) {

                return true
            }
        }
        return false
    }

    override fun onClick(v: View?) {
        when(v){
            iv_header_left -> {
                finish()
            }
            BtTijiao -> {
                updialog = Myutil.showupdatedialog(this, "正在上传", false)
                P.putOutsourcing()
            }
            BtShanchu -> {
                updialog = Myutil.showupdatedialog(this, "正在上传", false)
                P.deleteOutsourcing()
            }
        }
    }
    override fun putOutsourcingOk() {
        Myutil.MyTost("数据上传成功")
        updialog.dismiss()
        scans.clear()
        adapter.notifyDataSetChanged()
    }

    override fun putOutsourcingNo() {
        updialog.dismiss()
    }

    override fun deleteOutsourcingOk() {
        Myutil.MyTost("数据删除成功")
        updialog.dismiss()
        scans.clear()
        adapter.notifyDataSetChanged()
    }

    override fun deleteOutsourcingNo() {
        updialog.dismiss()
    }
}